package com.cloudfast.config;

import com.cloudfast.email.impl.JEmailDataSourceSenderImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.cloudfast.email.JEmailSender;
import com.cloudfast.email.impl.JEmailSenderImpl;
import org.springframework.mail.javamail.JavaMailSenderImpl;

/**
 * 发送邮件
 *
 * @author liuyw
 * @date 2022-10-22
 */
@Configuration
public class EmailConfiguration {

    @Autowired
    private EmailAuth emailAuth;
    @Autowired
    private EmailDataSource dataBaseSource;
    @Bean
    @ConditionalOnProperty(name = "spring.mail.type", havingValue = "default")
    public JEmailSender jEmail() {
        JavaMailSenderImpl jEmailSender = new JavaMailSenderImpl();
        jEmailSender.setHost(emailAuth.getHost());
        jEmailSender.setPassword(emailAuth.getPassword());
        jEmailSender.setUsername(emailAuth.getUsername());
        jEmailSender.setPort(emailAuth.getPort());
        return new JEmailSenderImpl(emailAuth, jEmailSender);
    }


    @Bean
    @ConditionalOnProperty(name = "spring.mail.type", havingValue = "datasource")
    public JEmailSender datasourceJEmail() {
        return new JEmailDataSourceSenderImpl(dataBaseSource);
    }
}
